﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChineseSchool.Models.Hethong;
using ChineseSchool.BLL;
using System.Data;

namespace ChineseSchool.Admin
{
    public partial class QL_CamNhan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BinGrid();
                btnAdd.Visible = true;
                btnEdit.Visible = true;
            }
        }

        protected void BinGrid()
        {
            var bCamNhan = new B_CamNhan();
            var lstMCamNhan = bCamNhan.GetAlls(0);
            grdCamNhan.DataSource = lstMCamNhan;
            grdCamNhan.DataBind();
        }

        protected void Bin_ddlUsers()
        {
            var dtMenu = (new B_User()).GetAlls();
            ddlUser.DataSource = dtMenu;
            ddlUser.DataValueField = "UserID";
            ddlUser.DataTextField = "UserName";
            ddlUser.DataBind();
        }

        protected void grdCamNhan_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdCamNhan.PageIndex = e.NewPageIndex;
            BinGrid();
        }

        protected void grdCamNhan_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var camNhanId = Convert.ToInt32(e.CommandArgument);
            var mCamNhan = new M_CamNhan();
            var bCamNhan = new B_CamNhan();
            var bInsertUpdateDelete = new B_Insert_Update_Delete();
            if (e.CommandName.Equals("Delete", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    bInsertUpdateDelete.delete("CamNhan", "CamNhanID", mCamNhan, camNhanId);
                    Response.Write("<script>alert('Đã được xóa!')</script>");
                }
                catch (Exception ex)
                {
                    throw new Exception("Đã có lỗi xảy ra! Vui lòng thử lại lần sau!", ex);
                }
                BinGrid();
            }
            if (e.CommandName.Equals("Edit", StringComparison.OrdinalIgnoreCase))
            {
                panAdd.Visible = true;
                btnAdd.Visible = false;
                lblTitle.Text = "Cập nhật";
                Bin_ddlUsers();
                mCamNhan = bCamNhan.GetByPK(camNhanId);
                var userId = Convert.ToInt32(mCamNhan.userID.ToString());
                if (userId == 0)
                {
                }
                {
                    ddlUser.SelectedValue = userId.ToString();
                }
                txtFacebook.Text = mCamNhan.facebook;
                txtSummary.Value = mCamNhan.tomTat;
                ckCamNhanContent.Value = mCamNhan.noiDung;
                imgDisplay.ImageUrl = mCamNhan.anhdaidien;
                if (mCamNhan.IsLock.ToString() == "True")
                {
                    rbYes.Checked = true;
                }
                else
                {
                    rbNo.Checked = true;
                }
                btnEdit.CommandArgument = camNhanId.ToString();
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            panAdd.Visible = true;
            btnEdit.Visible = false;
            lblTitle.Text = "Thêm mới";
            txtFacebook.Text = "";
            txtSummary.Value = "";
            ckCamNhanContent.Value = "";
            rbNo.Checked = true;
            Bin_ddlUsers();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var bInsertUpdateDelete = new B_Insert_Update_Delete();
            panAdd.Visible = false;
            btnEdit.Visible = true;
            /*tải ảnh */
            var imageUrl = "";
            var file2 = fuImageUrl.PostedFile;
            if (fuImageUrl.HasFile == false && file2.ContentLength > 500000)
            {
                lblThongbao.Text = "File nặng quá T_T";
            }
            else
            {
                try
                {
                    var path2 = Server.MapPath("/admin/images/Upload/" + fuImageUrl.FileName);
                    fuImageUrl.SaveAs(path2);
                    imageUrl = "/admin/images/Upload/" + fuImageUrl.FileName;
                    lblThongbao.Text = "Tải thành công";
                }
                catch
                {
                    lblThongbao.Text = "Tải ảnh thất bại! vui lòng tải lại";
                }
            }

            /*tải ảnh */
            var mCamNhan = new M_CamNhan();            
            var userId = 0;
            if (ddlUser.SelectedValue != "")
            {
                userId = Convert.ToInt32(ddlUser.SelectedValue);
            }
            var user = (new B_User()).Selectby_id(userId);
            mCamNhan.userID = userId;
            //mCamNhan.anhdaidien = user.Avatar;
            mCamNhan.anhdaidien = imageUrl;
            mCamNhan.email = user.Email;
            mCamNhan.hoten = user.FullName;
            mCamNhan.ngaySinh = user.Birthday;
            mCamNhan.facebook = txtFacebook.Text;
            mCamNhan.tomTat = txtSummary.Value;
            mCamNhan.noiDung = ckCamNhanContent.Value;
            mCamNhan.IsLock = rbYes.Checked;        
            bInsertUpdateDelete.insert("CamNhan", "CamNhanID", mCamNhan);
            panAdd.Visible = false;
            BinGrid();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            var bInsertUpdateDelete = new B_Insert_Update_Delete();
            panAdd.Visible = false;
            btnAdd.Visible = true;
            var imageUrl = "";
            var mCamNhan = new M_CamNhan();
            var bCamNhan = new B_CamNhan();
            var camnhanId = Convert.ToInt32(btnEdit.CommandArgument);
            var file2 = fuImageUrl.PostedFile;
            if (fuImageUrl.HasFile)
            {
                if (fuImageUrl.HasFile == false && file2.ContentLength > 500000)
                {
                    lblThongbao.Text = "File nặng quá T_T";
                }
                else
                {
                    try
                    {
                        string path2 = Server.MapPath("/admin/images/Upload/" + fuImageUrl.FileName);
                        fuImageUrl.SaveAs(path2);
                        imageUrl = "/admin/images/Upload/" + fuImageUrl.FileName;
                        lblThongbao.Text = "Tải thành công";
                    }
                    catch
                    {
                        lblThongbao.Text = "Tải ảnh thất bại! vui lòng tải lại";
                    }
                }
            }
            else
            {
                mCamNhan = bCamNhan.GetByPK(camnhanId);
                imageUrl = mCamNhan.anhdaidien;
            }
            /*tải ảnh */
            mCamNhan.camnhanID = Convert.ToInt32(btnEdit.CommandArgument);            
            var userId = 0;
            if (ddlUser.SelectedValue != "")
            {
                userId = Convert.ToInt32(ddlUser.SelectedValue);
            }
            var user = (new B_User()).Selectby_id(userId);
            mCamNhan.userID = userId;
            mCamNhan.anhdaidien = imageUrl;
            mCamNhan.email = user.Email;
            mCamNhan.hoten = user.FullName;
            mCamNhan.ngaySinh = user.Birthday;
            mCamNhan.facebook = txtFacebook.Text;
            mCamNhan.tomTat = txtSummary.Value;
            mCamNhan.noiDung = ckCamNhanContent.Value;
            mCamNhan.IsLock = rbYes.Checked;        
            bInsertUpdateDelete.update("CamNhan", "CamNhanID", mCamNhan, camnhanId);
            panAdd.Visible = false;
            BinGrid();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            panAdd.Visible = false;
            btnAdd.Visible = true;
            btnEdit.Visible = true;
            txtFacebook.Text = "";
            txtSummary.Value = "";
            rbNo.Checked = true;
            ckCamNhanContent.Value = "";
            BinGrid();
        }

        protected void grdCamNhan_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            BinGrid();
        }

        protected void grdCamNhan_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdCamNhan.EditIndex = e.NewEditIndex;
            BinGrid();
        }
    }
}